package com.yupi.springbootinit.utils;

import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import net.coobird.thumbnailator.Thumbnails;

/**
 * 图片压缩工具类
 */
public class ThumbNailUtil {

    public static void thumbnail(MultipartFile file,String filePath,String imgType) {
        try {
            // 先尝试压缩并保存图片
            Thumbnails.of(file.getInputStream()).scale(0.5f)
                    .outputQuality(0.15f)
                    .outputFormat(imgType)
                    .toFile(filePath);
        } catch (IOException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,e.getMessage());
        }
    }

}
